<?php
namespace App\Models\DyModels;
use App\Models\ParentModel;
use DB;
/**
 * 抖音视频评论model
 * @author puppet
 */
class KeywordVideoComment extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_keyword_video_comment';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_keyword_video_comment';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
  /**
   * 翻页展示所有信息
   * 关键词视频评论拉取
   */
	public function getPageData($params){
		$limit_num = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$limit_sta = 0;
		$page = 1;
		if (isset($params['page']) && intval($params['page']) > 0) {
			$limit_sta = intval($limit_num * intval($params['page'] - 1));
			$page = intval($params['page']);
		}
		$cou_sql = "SELECT count(*) as count FROM {$this->db_table} WHERE 1 = ?";
		$sql = "SELECT id,nickname,signature,user_id,content,create_time,update_time,userImg,video_url,sec_uid,status,tells,title,keyword FROM {$this->db_table} WHERE 1 = ?";

		if(isset($params['content']) && !empty($params['content'])){
			$cou_sql .= " AND content like '%{$params['content']}%'";
			$sql .= " AND content like '%{$params['content']}%'";
		}
		
		if(isset($params['start_date']) && !empty($params['start_date'])){
            $params['start_date'] = $params['start_date'] . ' 00:00:00';
			$cou_sql.=" and update_time >= '{$params['start_date']}'";
			$sql.=" and update_time >= '{$params['start_date']}'";
		}
		
		if(isset($params['end_date']) && !empty($params['end_date'])){
            $params['end_date'] = $params['end_date'] . ' 23:59:59';
			$cou_sql.=" and update_time <= '{$params['end_date']}'";
			$sql.=" and update_time <= '{$params['end_date']}'";
		}
		
		if(isset($params['status']) && $params['status']!='99'){
			$cou_sql.=" and status = {$params['status']}";
			$sql.=" and status = {$params['status']}";
		}
		
		if(isset($params['order']) && !empty($params['order'])){
			$order_db = ['id'];
			$sql .= " ORDER BY ";
			foreach ($params['order'] as $order){
				$result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
				!empty($result) && $sql .= $result.' '.$order['dir'].',';
			}
			$sql = substr($sql,0,-1);
		}
		// echo $sql;exit;
		$sql .= " LIMIT $limit_sta,$limit_num";
		$count = DB::select($cou_sql,[1]);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$order_list = DB::select($sql,[1]);
		if($order_list){
			foreach($order_list as $k=>$v){
				$order_list[$k]['content']="<div style=\"width: 200px;height: 80px;overflow: auto;\">".htmlspecialchars_decode($v['content'])."</div>";
				$order_list[$k]['userImg']="<a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" style=\"width:80px;height:80px;\" ></a>";
				$order_list[$k]['signature']="<div style=\"width: 200px;height: 80px;overflow: auto;\">".htmlspecialchars_decode($v['signature'])."</div>";
				$order_list[$k]['video_url']="<a href=\"{$v['video_url']}\" target=\"_blank\">".substr_replace($v['video_url'],'...',15)."</a>";
				$order_list[$k]['title']="<div style=\"width: 120px;height: 80px;overflow: auto;\">{$v['title']}</div>";
				$status=$v['status']==0 ? 1 : 0;
				$order_list[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"".($status==1?'default':'checkbox')."\" checked=\"\" onclick=\"actionList({$v['id']},{$status})\"><span></span></label></div>";
				
				$order_list[$k]['tells']="<div style=\"width: 120px;height: 80px;overflow: auto;\">";
					$order_list[$k]['tells'].="<textarea style=\"width:100%;\">{$v['tells']}</textarea><br/>";
					$order_list[$k]['tells'].="<span style=\"border:1px solid;padding:3px;background-color:#5cb85c;color:#fff;cursor:pointer;\" onclick=\"savTells(this,{$v['id']})\">保存</span>";
				$order_list[$k]['tells'].="</div>";
			}
		}
		return ['data'=>$order_list,'total'=>$count];
	}
	
	function upKCStatus($kid,$status){
		$sql="update {$this->table} set `status`={$status} where id={$kid}";
		return DB::update($sql);
	}
	
	//作品雷达评论数据拉取
	function getInfoData($params){
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$offset = 0;
		$page = 1;
		if (isset($params['page']) && intval($params['page']) > 0) {
			$offset = intval($size * intval($params['page'] - 1));
			$page = intval($params['page']);
		}
		
		$countSql="select count(id) as count from pt_user_info_comment where 1=?";
		$sql="select * from pt_user_info_comment where 1=?";
		
		if(isset($params['content']) && !empty($params['content'])){
		    $countSql.=" and content like '%{$params['content']}%'";
		    $sql.=" and content like '%{$params['content']}%'";
		}
		
		if(isset($params['start_date']) && !empty($params['start_date'])){
            $params['start_date'] = $params['start_date'] . ' 00:00:00';
			$countSql.=" and update_time >= '{$params['start_date']}'";
			$sql.=" and update_time >= '{$params['start_date']}'";
		}
		
		if(isset($params['end_date']) && !empty($params['end_date'])){
            $params['end_date'] = $params['end_date'] . ' 23:59:59';
			$countSql.=" and update_time <= '{$params['end_date']}'";
			$sql.=" and update_time <= '{$params['end_date']}'";
		}
		
		if(isset($params['search_title']) && !empty($params['search_title'])){
		    $countSql.=" and title like '%{$params['search_title']}%'";
		    $sql.=" and title like '%{$params['search_title']}%'";
		}

        if(isset($params['order']) && !empty($params['order'])){
            $order_db = ['id'];
            $sql .= " ORDER BY ";
            foreach ($params['order'] as $order){
                $result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
                !empty($result) && $sql .= $result.' '.$order['dir'].',';
            }
            $sql = substr($sql,0,-1);
        }
		$sql.="  limit $offset,$size";
		$count=DB::select($countSql,[1]);
		$count=isset($count[0]['count']) ? $count[0]['count'] : 0;
		$order_list=DB::select($sql,[1]);
		if($order_list){
			foreach($order_list as $k=>$v){
				$order_list[$k]['content']="<div style=\"width: 200px;height: 80px;overflow: auto;\">".htmlspecialchars_decode($v['content'])."</div>";//.htmlspecialchars_decode特殊字符换成普通字符
				$order_list[$k]['userImg']="<a href=\"https://www.douyin.com/user/{$v['sec_uid']}\" target=\"_blank\"><img src=\"{$v['userImg']}\" style=\"width:80px;height:80px;\" ></a>";
				$order_list[$k]['signature']="<div style=\"width: 200px;height: 80px;overflow: auto;\">".htmlspecialchars_decode($v['signature'])."</div>";
				$order_list[$k]['video_url']="<a href=\"{$v['video_url']}\" target=\"_blank\">".substr_replace($v['video_url'],'...',15)."</a>";
				$order_list[$k]['title']="<div style=\"width: 120px;height: 80px;overflow: auto;\">{$v['title']}</div>";
				$order_list[$k]['tells']="<div style=\"width: 120px;height: 80px;overflow: auto;\">";
					$order_list[$k]['tells'].="<textarea style=\"width:100%;\">{$v['tells']}</textarea><br/>";
					$order_list[$k]['tells'].="<span style=\"border:1px solid;padding:3px;background-color:#5cb85c;color:#fff;cursor:pointer;\" onclick=\"savTells(this,{$v['id']})\">保存</span>";
				$order_list[$k]['tells'].="</div>";
			}
		}
		return ['data'=>$order_list,'total'=>$count];
	}
	
	//修改作品评论联系方式
	function infoCommentEdit($kid,$data){
		return DB::table("pt_user_info_comment")->where($this->primaryKey,$kid)->update($data);
	}
}
